package com.gin.no_gay_alliance.viewmodel;

import android.app.DownloadManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.field.Attachment;
import com.gin.no_gay_alliance.utils.ImageUtils;

import java.util.List;

/**
 * 已上传的附件
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/29 09:07
 **/
public class AttachmentModel implements Parcelable, ImageUtils.PreviewSource {
    public static final Creator<AttachmentModel> CREATOR = new Creator<AttachmentModel>() {
        @Override
        public AttachmentModel createFromParcel(Parcel in) {
            return new AttachmentModel(in);
        }

        @Override
        public AttachmentModel[] newArray(int size) {
            return new AttachmentModel[size];
        }
    };
    /**
     * 图片相对地址
     */
    public String url;
    /**
     * 扩展名/后缀名
     */
    public String extension;
    public String filename;
    public String type;
    public String originalFilename;
    public List<String> thumbUrls;
    public Long size;
    /**
     * 用于下载勾选
     */
    public boolean checked = true;

    public AttachmentModel(Attachment attachment) {
        this.url = attachment.getUrl();
        this.filename = attachment.getFilename();
        this.type = attachment.getType();
        this.originalFilename = attachment.getOriginalFilename();
        this.thumbUrls = attachment.getThumbUrls();
        this.size = attachment.getSize();
        this.extension = attachment.getExtension();
        if (ObjectUtils.isEmpty(this.extension) && this.filename != null) {
            this.extension = this.filename.substring(this.filename.lastIndexOf(".") + 1);
        }
    }

    protected AttachmentModel(Parcel in) {
        url = in.readString();
        extension = in.readString();
        filename = in.readString();
        type = in.readString();
        originalFilename = in.readString();
        thumbUrls = in.createStringArrayList();
        if (in.readByte() == 0) {
            size = null;
        } else {
            size = in.readLong();
        }
    }

    /**
     * 构造下载请求
     *
     * @return 下载请求
     */
    public DownloadManager.Request buildDownloadRequest() {
        String url = Attachment.PREFIX + this.url;
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "no_gay/" + getDisplayName());
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        return request;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(url);
        dest.writeString(extension);
        dest.writeString(filename);
        dest.writeString(type);
        dest.writeString(originalFilename);
        dest.writeStringList(thumbUrls);
        if (size == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(size);
        }
    }

    /**
     * 提供用于下载的文件名
     *
     * @return 下载文件名
     */
    public String getDisplayName() {
        if (!ObjectUtils.isEmpty(originalFilename)) {
            return originalFilename;
        }
        return filename;
    }

    @Override
    public String getPreviewUrl() {
        return Attachment.PREFIX + url;
    }
}
